package cn.justforchris.system.controller;



import cn.justforchris.system.annotation.PreAuthorize;
import cn.justforchris.system.domain.Permission;
import cn.justforchris.system.query.PermissionQuery;
import cn.justforchris.system.service.IPermissionService;
import cn.justforchris.util.JsonResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 权限接口
 */
@RestController
@RequestMapping("/permission")
@Api(tags = "权限接口")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    /**
     * 新增
     * @param
     * @return
     */
    @PutMapping
    @PreAuthorize(name="保存权限",sn="permission:save")
    public JsonResult insertOrUpdate(@RequestBody Permission permission) {
       try {
           Date date = new Date();

           if (permission.getId() == null) {
            permissionService.insertPermission(permission);
        } else {
            permissionService.updatePermission(permission);
        }
        return JsonResult.success();
       }catch (Exception e) {
           e.printStackTrace();
           return JsonResult.error();
       }
    }

    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @PreAuthorize(name="删除权限",sn="permission:delete")
    public JsonResult deletPermission(@PathVariable Long id){
        try{
            permissionService.deletePermission(id);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @PreAuthorize(name="批量删除权限",sn="permission:patchDelete")
    public JsonResult patchDelet(@RequestBody Long[] ids){
        try{
            permissionService.patchDelete(ids);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 根据id获取数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @PreAuthorize(name="查询单个权限",sn="permission:query")
    public Permission getPermission(@PathVariable Long id){

        return permissionService.getPermissionById(id);

    }

    /**
     * 获取所有
     * @return
     */
    @GetMapping
    @PreAuthorize(name="查询所有权限",sn="permission:query")
    public List<Permission> getAll(){
        return permissionService.getAll();
    }

    /**
     * 分页查询
     * @param
     * @return
     */
    @PostMapping
    @PreAuthorize(name="分页查询权限",sn="permission:query") /*自定义注解,用于获取权限信息,判断数据库中是否有该权限,如果没有则写入数据库*/
    public PageInfo<Permission> queryPage(@RequestBody PermissionQuery permissionQuery){
        /*返回的数据是pagehelper的一个类,这个类创建的对象中储存的是查询出来的数据以及总条数(total)*/
        return permissionService.queryPage(permissionQuery);

    }






}
